<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\AdsRequest;
use App\Http\Controllers\Controller;
use App\Model\Ads;
use DB,Session,Input,File;

class AdsController extends Controller
{
    protected $position = [
        '幻灯片',
        '商品分类',
        '热门活动',
        '楼层广告'
    ];
    
    // 显示广告列表
    public function index(Request $request)
    {
        $ads = Ads::where('ads_status', '<>', '-1')
                ->orderBy('id', 'desc')
                ->paginate(15);
        
        return view('admin.ads.index', ['ads'=>$ads, 'position'=>$this->position]);
    }
    
    // 添加数据
    public function create()
    {
        return view('admin.ads.add', ['position'=>$this->position]);
    }
    
    // 保存数据
    public function store(AdsRequest $request)
    {
        $data = $request->except('_token');
        $res = Ads::insert($data);
        if(false !== $res){
            return redirect('/admin/ads');
        } else {
            return back()->with(['error'=>'添加失败']);
        }
    }
    
    // 修改页面
    public function edit($id)
    {
        $data = Ads::where('id', $id)
                ->where('ads_status', '<>', '-1')
                ->first();
        return view('admin.ads.edit', ['data'=>$data, 'position'=>$this->position]);
    }
    
    // 修改数据
    public function update(AdsRequest $request, $id)
    {
        $data = $request->except('_token', '_method');
        
        $res = Ads::where('id', $id)->update($data);
        
        if(false !== $res){
            return redirect('/admin/ads');
        } else {
            return back()->with(['error'=>'修改失败']);
        }
    }
    
    // 删除数据
    public function destroy($id=0)
    {
        $ads = new Ads();
        $data = ['ads_status'=>-1];
        if($id == 0){
            $id = Input::get('id');
        }
        if(is_array($id)){
            $action = '批量';
            $res = $ads->whereIn('id', $id)->update($data);
        } else {
            $action = '';
            $res = $ads->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '删除成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '删除失败']);
        }
    }
    
    // 上传图片
    public function uploadone(Request $request){
        // 获取图片，并上传
        $file = $request->file('mallPic');
        $suffix = $file->getClientOriginalExtension();
        $savepath = './uploads/ads/' . date('Ymd');
        if(!is_dir($savepath)){
            File::makeDirectory($savepath,  0777, true);
        }
        
        $rename = date('YmdHis') . mt_rand(1000,9999) . '.' . $suffix;
        $result = $file->move($savepath, $rename);
        
        if($result){
            $bigImg = $savepath . '/' . $rename;
            // 返回
            return response()->json(['savepath'=>ltrim($bigImg,'.'), 'thumbsavepath'=>ltrim($bigImg,'.')]);
        } else {
            return response()->json(['savepath'=>'','thumbsavepath'=>'']);
        }
    }
    
    // 开启关闭
    public function isopen()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        
        $ads = new Ads();
        $data = ['ads_status'=>$status];
        
        if($status == 1){
            $action = '开启';
        } else {
            $action = '关闭';
        }
        
        if(is_array($id)){
            $action = '批量' . $action;
            $res = $ads->whereIn('id', $id)->update($data);
        } else {
            $res = $ads->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '失败']);
        }
    }
}
